package com.myopen.controller.admin;


import java.util.Map;


import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.myopen.base.BaseController;
import com.myopen.base.BaseDataTableVo;
import com.myopen.base.BaseVo;
import com.myopen.config.ConstConfig;
import com.myopen.controller.AlipayController;
import com.myopen.controller.WeixinPayController;
import com.myopen.kit.IpKit;
import com.myopen.model.Log;
import com.myopen.model.Order;

public class OrderController extends BaseController{

	@ActionKey("/admin/shop/order")
	public void index() {
		render("/admin/shop/order/index.html");
	}
	
	@ActionKey("/admin/shop/order/pagequery")
	public void pagequery(){
		
		Record record=getSearchRecord();//会自动组装通用的参数
		record.set("searchContent", getPara("searchContent"));
		record.set("status", getPara("status"));
		Page<Record> page=Order.dao.pagequery(record);
		
		BaseDataTableVo tableVo=new BaseDataTableVo(page);
		renderJson(tableVo);
	}
	
	@ActionKey("/admin/shop/order/view")
	public void view(){
		
		String id=getPara("id");

		Record order=Order.dao.getRecordById(id);

		setAttr("vo", order);
		render("/admin/shop/order/detail.html");
	}

	@ActionKey("/admin/shop/order/save")
	public void save(){

		BaseVo baseVo=new BaseVo();
		baseVo.setCode("0");

		renderJson(baseVo);
	}

	@Before(Tx.class)
	@ActionKey("/admin/shop/order/verify")
	public void verify(){
		
		BaseVo baseVo=new BaseVo();
		Record currUser=getAdminUser();
		String id=getPara("id");
		Order.dao.verify(id, currUser.get("id"));
		//添加日志
		Log.dao.addShowLog(currUser.get("id"), "订单确认，订单ID："+id, "order_verify");

		baseVo.setCode("0");
		renderJson(baseVo);
	}

	@Before(Tx.class)
	@ActionKey("/admin/shop/order/delivery")
	public void delivery(){
		
		BaseVo baseVo=new BaseVo();
		Record currUser=getAdminUser();
		String id=getPara("id");
		Order.dao.delivery(id, currUser.get("id"));
		//添加日志
		Log.dao.addShowLog(currUser.get("id"), "订单发货，订单ID："+id, "order_delivery");

		baseVo.setCode("0");
		renderJson(baseVo);
	}

	@Before(Tx.class)
	@ActionKey("/admin/shop/order/cancel")
	public void cancel(){
		
		BaseVo baseVo=new BaseVo();
		Record currUser=getAdminUser();
		String id=getPara("id");
		Order.dao.delivery(id, currUser.get("id"));
		//添加日志
		Log.dao.addShowLog(currUser.get("id"), "订单取消，订单ID："+id, "order_cancel");

		baseVo.setCode("0");
		//取消订单单独加一个功能，有时候订单取消了，但是不一定退款
		renderJson(baseVo);
	}

	@Before(Tx.class)
	@ActionKey("/admin/shop/order/refund")
	public void refund(){
		
		BaseVo baseVo=new BaseVo();
		Record currUser=getAdminUser();

		String id=getPara("id");
		Record order=Order.dao.getRecordById(id);
		Record payHistory=Order.dao.getPayHistorySuccessByOrderId(order.getStr("id"));

		if(!"1".equals(payHistory.getStr("payStatus"))){
			//如果不等于1，那么不能退款，1为付款成功
			baseVo.setCode("99");
			baseVo.setMsg("该订单不能退款！");
		}
		
		String payType=payHistory.getStr("payType");

		if(ConstConfig.pay_alipay.equals(payType)){

			Map<String,Object> result=new AlipayController().alipay_refund(id);
			Map<String,Object> response=(Map<String,Object>)result.get("alipay_trade_refund_response");
			if("10000".equals(response.get("code"))){
				baseVo.setCode("0");
				Order.dao.refund(id,currUser.get("id"));
				//添加日志
				Log.dao.addShowLog(currUser.get("id"), "订单退款，订单ID："+id, "order_refund");
			}else{
				baseVo.setCode("99");
				baseVo.setMsg((String)response.get("msg"));
			}
		}else if(ConstConfig.pay_wxpay.equals(payType)){

			//微信退款需要用到证书，证书为一个文件
			String path=getRequest().getServletContext().getRealPath("/");
			Map<String,String> resultMap=new WeixinPayController().weixinpay_refund(id, path);
			String return_code=resultMap.get("return_code");
			String result_code=resultMap.get("result_code");
			boolean flag=false;
			if("SUCCESS".equals(return_code)){
				if("SUCCESS".equals(result_code)){
					Order.dao.refund(id,currUser.get("id"));
					//添加日志
					Log.dao.addShowLog(currUser.get("id"), "订单退款，订单ID："+id, "order_refund");
					flag=true;
				}
			}

			if(flag){
				baseVo.setCode("0");
			}else{
				baseVo.setCode("99");
			}

		}

		renderJson(baseVo);
	}

	//订单完成
	@ActionKey("/admin/shop/order/complete")
	public void complete(){
		
		BaseVo baseVo=new BaseVo();
		Record currUser=getAdminUser();
		String id=getPara("id");
		Order.dao.complete(id, currUser.get("id"));
		//添加日志
		Log.dao.addShowLog(currUser.get("id"), "订单完成，订单ID："+id, "order_complete");
		
		baseVo.setCode("0");
		renderJson(baseVo);
	}
	
}
